package divtastic.f9
{
    
    import divtastic.Pages;
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    import flash.events.Event;
    
    
    
    public class NextTimer
    {
        
        public static function RUN( pages: Pages, cycle_time: int, max_loops: int = 1 )
        {
            
            var counter:    int             = 0;
            var maxLoops:   int             = max_loops;
            var pg:         Pages           = pages;
            pg.circle                       = true;
            var loopTime:   int             = cycle_time;
            
            var nextFunc:   Function        =   function()
                                                {
                                                    
                                                    if( pages.circle == false ) return;
                                                    pg.next();
                                                    OneTime.RUN( nextFunc, loopTime );
                                                    
                                                };
            
            
            if( maxLoops != -1 )
            {
                var finishLoop: Function        =   function()
                                                    {
                                                    
                                                        counter++;
                                                        trace( 'nextcounter ' + counter );
                                                        if( counter == maxLoops )
                                                        {
                                                        
                                                            pg.circle = false;
                                                            pg.looped.remove( finishLoop );
                                                        
                                                        }
                                                    
                                                    };
            }
            OneTime.RUN( nextFunc, loopTime );
            if( maxLoops != -1 )
            {
                
                pg.looped.add( finishLoop );
                
            }
        }
        
    }
    
    
}